package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.common.dtas.BaseServiceImpl;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper, AdSensitive>implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1检查参数
        if (dto==null)return ErrorParam;
        Page page = new Page(dto.getPage(), dto.getSize());
        //2.模糊分页查询
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        IPage page1 = page(page, wrapper);
        //分装返回结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page1.getTotal());
        responseResult.setData(page1.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult inster(AdSensitive adSensitive) {
        if (adSensitive==null)return ErrorParam;
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return Success;
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive==null)return ErrorParam;
        AdSensitive sensitive = getById(adSensitive);
        if (sensitive.getSensitives().equals(adSensitive))return DatA;
        updateById(adSensitive);
        return Success;
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id==null)return ErrorParam;
        removeById(id);
        return Success;
    }
}
